// pages/category/category.js
Page({
  /**
   * 页面的初始数据
   */
  data: {
    value: "",
    swiperOptions:{
      indicatorDots:true,
      autoplay:true,
      interval:3000,
      duration:1000,
      circular:true,    
  },
    categoryList: [],
    windowHeight: '',
    currentIndex: 0, // 左侧分类菜单选中索引
    toView: '', // 右侧自动滑动到的id
    rightScrollTop: '', // 右侧滚动高度
    rightTopArray: [], // 记录右侧条目的顶部值
    active:[1,0,0,0],
    dog:[{
      num:1,
      price:70.00,
      desc:"还你可爱小狗",
      title:"小狗洗护",
      thumb:"/images/dog.JPG"
    },{
      num:1,
      price:120.00,
      desc:"中小型犬",
      title:"狗狗上门喂养",
      thumb:"/images/dog1.JPG"
    }],
    cat:[{
      num:1,
      price:60.00,
      desc:"描述信息",
      title:"小猫洗护",
      thumb:"/images/cleanCat.JPG"
    },{
      num:1,
      price:120.00,
      desc:"描述信息",
      title:"猫猫上门喂养",
      thumb:"/images/catEat.JPG"
    }],
    car:[{
      num:1,
      price:60.00,
      desc:"描述信息",
      title:"猫猫约车",
      thumb:"/images/carCat.JPG"
    },{
      num:1,
      price:80.00,
      desc:"描述信息",
      title:"狗狗约车",
      thumb:"/images/carDog.JPG"
    }],
    service:[{
      num:1,
      price:100.00,
      desc:"描述信息",
      title:"宠物营养餐",
      thumb:"/images/service.jpg"
    },{
      num:1,
      price:200.00,
      desc:"描述信息",
      title:"养宠家庭卫生打扫",
      thumb:"/images/goDog.JPG"
    },
    {
      num:1,
      price:200.00,
      desc:"描述信息",
      title:"养宠家庭卫生打扫",
      thumb:"/images/goCat.JPG"
    },{
      num:1,
      price:200.00,
      desc:"做最懂养宠人的程序",
      title:"定制化家庭服务",
      thumb:"/images/service.jpg"
    }]
  },  
  currentList:[],
  
  /**点击搜索框获取焦点*/
  clickSearch(){
    wx.navigateTo({
      url: '/pages/search/search',
    })
  },
  dogClick(e){
    this.setData({
      active:[1,0,0,0],
      currentList:this.data.dog
    })
  },
  catClick(e){
    this.setData({
      active:[0,1,0,0],
      currentList:this.data.cat
    })
  },
  // test11(){
  //   console.log(111)
  // },
  carClick(e){
    wx.navigateToMiniProgram({  
      appId: 'wxaf35009675aa0b2a' // 替换为目标小程序的 appId     // 可选，目标小程序的页面路径  
    });  
    this.setData({
      active:[0,0,1,0],
      currentList:this.data.car
    })
  },
  serviceClick(e){
    this.setData({
      active:[0,0,0,1],
      currentList:this.data.service
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.data.categoryList=getApp().globalData.category;
    this.setData({
       categoryList: this.data.categoryList
    })
    this.setData({
      currentList:[{
        num:3,
        price:70.00,
        desc:"还你可爱小狗",
        title:"小狗洗护",
        thumb:"/images/dog.JPG"
      },{
        num:1,
        price:120.00,
        desc:"中小型犬",
        title:"狗狗上门喂养",
        thumb:"/images/dog1.JPG"
      }]
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},

  // 左侧分类菜单点击事件
  clickCategory(e) {
    const index = e.currentTarget.dataset.index;
    if (index !== this.data.currentIndex) {
      this.setData({
        currentIndex: index,
        toView: this.data.categoryList[index].id
      });
    }
  },
  // 右侧滚动事件
  rightScroll(e) {
    const scrollTop = e.detail.scrollTop;
    const { rightTopArray, currentIndex } = this.data;
    const topOne = rightTopArray[0] || 0;
    const length = rightTopArray.length;

    for (let i = 0; i < length; i++) {
      const top = rightTopArray[i];
      if (i < length - 1) {
        const topNext = rightTopArray[i + 1];
        if (scrollTop >= top - topOne && scrollTop < topNext - topOne) {
          if (currentIndex !== i) {
            this.setData({
              currentIndex: i
            });
          }
          break;
        }
      } else {
        if (scrollTop >= top - topOne) {
          if (currentIndex !== i) {
            this.setData({
              currentIndex: i
            });
          }
          break;
        }
      }
    }
  },
});